package io.agora.openduo.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import io.agora.openduo.MyApp;
import io.agora.openduo.R;
import io.agora.openduo.agora.EngineEventListener;
import io.agora.openduo.databinding.ActivityMainBinding;
import io.agora.openduo.utils.FileUtil;
import io.agora.openduo.utils.LogUtil;
import io.agora.rtc.Constants;
import io.agora.rtc.RtcEngine;
import io.agora.rtc.video.CameraCapturerConfiguration;
import io.agora.rtm.ErrorInfo;
import io.agora.rtm.ResultCallback;
import io.agora.rtm.RtmClient;

/**
 * @author jiangyao
 * @Date: 2022/5/5
 * @Email: jiangmr@vip.qq.com
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    ActivityMainBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        binding.phone.setOnClickListener(view -> {
            MyApp.mConfig.userID = "1018";
            initEngine();
            startActivity(new Intent(this, DialerActivityAgora.class));
        });

        binding.device.setOnClickListener(view -> {
            MyApp.mConfig.isDev = true;
            MyApp.mConfig.userID = "1020";
            initEngine();
            startActivity(new Intent(this, DialerActivityAgora.class));
        });
    }

    private void initEngine() {
        String appId = getString(R.string.private_app_id);
        if (TextUtils.isEmpty(appId)) {
            throw new RuntimeException("NEED TO use your App ID, get your own ID at https://dashboard.agora.io/");
        }

        MyApp.mEventListener = new EngineEventListener();
        try {
            MyApp.mRtcEngine = RtcEngine.create(getApplicationContext(), appId, MyApp.mEventListener);
            MyApp.mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
            MyApp.mRtcEngine.enableDualStreamMode(true);
            MyApp.mRtcEngine.enableVideo();
            MyApp.mRtcEngine.setLogFile(FileUtil.rtmLogFile(getApplicationContext()));

            //默认后置相机  RGB
            MyApp.mRtcEngine.setCameraCapturerConfiguration(
                    new CameraCapturerConfiguration(
                            CameraCapturerConfiguration.CAPTURER_OUTPUT_PREFERENCE.CAPTURER_OUTPUT_PREFERENCE_AUTO,
                            CameraCapturerConfiguration.CAMERA_DIRECTION.CAMERA_REAR
                    )
            );

            MyApp.mRtmClient = RtmClient.createInstance(getApplicationContext(), appId, MyApp.mEventListener);
            MyApp.mRtmClient.setLogFile(FileUtil.rtmLogFile(getApplicationContext()));

            MyApp.rtmCallManager = MyApp.mRtmClient.getRtmCallManager();
            MyApp.rtmCallManager.setEventListener(MyApp.mEventListener);

            String accessToken = getString(R.string.rtm_access_token);
            if (TextUtils.equals(accessToken, "") || TextUtils.equals(accessToken, "<#YOUR ACCESS TOKEN#>")) {
                accessToken = null;
            }
            MyApp.mRtmClient.login(accessToken, MyApp.mConfig.userID, new ResultCallback<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    LogUtil.e(TAG, "rtm client login success");
                }

                @Override
                public void onFailure(ErrorInfo errorInfo) {
                    LogUtil.e(TAG, "rtm client login failed:" + errorInfo.getErrorDescription());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
